
import MathTool from "../../GzFrameWork/Tool/MathTool";
import { BaseState } from "../../GzFrameWork/StateMachine/BaseState";
import TimeTool from "../../GzFrameWork/Tool/TimeTool";
import AStarMapManager from "../AStart/AStarMapManager";
import AStarPoint from "../AStart/ASatrPoint";



export class BaseWalkState extends BaseState 
{
    constructor(gamerole,id)
    {
        super(gamerole,id);
        this.m_astarMap=AStarMapManager.GetInstance();
    }

    protected m_astarMap:AStarMapManager;
    protected m_forwardDir:Laya.Vector3=new Laya.Vector3();
    protected m_rightDir:Laya.Vector3=new Laya.Vector3();
    protected m_moveDir:Laya.Vector3=new Laya.Vector3();
    protected m_upDir=new Laya.Vector3(0,1,0);
  
    public m_MoveForce:number=120000;//驱动力
    protected m_moveSpeed:number=10;
    protected m_posPathArray:Array<Laya.Vector3>=[];



    protected  AddPath(end:AStarPoint):void
    {
        this.m_posPathArray=[];
        let temp:AStarPoint = end;
        let orilPoint:AStarPoint = null;
        //console.log("AddPath=====================");
        while (true)
        {
            this.m_posPathArray.push(temp.Pos);
            if (temp.Parent==null)
            {
            //  temp.ChangeColor(1,0,0,1);
              break;
            }
                
            orilPoint=temp;
            temp = temp.Parent;
            orilPoint.Parent=null;
        }
        this.m_posPathArray.reverse();
    }


    public SetTargetPath(targetPos:Laya.Vector3)
    {
        this.m_posPathArray=[];
        let endpoint =this.m_astarMap.GetPointByPos(targetPos);
        endpoint.ChangeColor(0,1,1,1);
        if (endpoint==null||endpoint.IsWall)
            return;
         //console.log("SetTargetPath");
        let startPoint=this.m_astarMap.GetPointByPos(this.m_stateController.MyTransform.position);
        this.m_astarMap.FindPath(startPoint,endpoint);
        this.AddPath(endpoint);  
    }


    OnEnter(param?:Object) 
    {

    }

    Act() 
    {
      

    }


    onExit() 
    {
        
    }


    // protected  MoveByForce()
    // {
    //     if (this.m_posPathArray.length<1) {
    //         return;
    //     }
    //     let targetpos =new Laya.Vector3(this.m_posPathArray[0].x,this.m_stateController.MyTransform.position.y, this.m_posPathArray[0].z);
    //     this.m_stateController.MyTransform.lookAt(targetpos,this.m_upDir);
    //     let forward:Laya.Vector3=new Laya.Vector3();
    //     this.m_stateController.MyTransform.getForward(forward);
    //     let  forwardDir:Laya.Vector3=new Laya.Vector3();
    //     Laya.Vector3.normalize(forward,forwardDir);
    //     //施加力
    //     let forwardAddForce=new Laya.Vector3();     
    //     Laya.Vector3.scale(forwardDir,this.m_MoveForce*TimeTool.DeltaTime,forwardAddForce);
    //     forwardAddForce.y=-1000;
    //     this.m_stateController.MyRigibody.applyForce(forwardAddForce);

    //     let velocityscaler=Laya.Vector3.scalarLength( this.m_stateController.MyRigibody.linearVelocity);

    //     let velocityNormalize=new Laya.Vector3();
    //     Laya.Vector3.normalize(this.m_stateController.MyRigibody.linearVelocity,velocityNormalize);
       
      
    //     if (velocityscaler>this.m_moveSpeed)
    //     { 
    //         let velocity=new Laya.Vector3();
    //         Laya.Vector3.scale(velocityNormalize,this.m_moveSpeed,velocity);
    //         velocity.y=this.m_stateController.MyRigibody.linearVelocity.y;
    //         this.m_stateController.MyRigibody.linearVelocity = velocity;
    //     }
    //     else if (velocityscaler<5) 
    //     {
    //         this.m_stateController.MyRigibody.friction=0;
    //     }
    //     else
    //     {
    //         let angle=Laya.Vector3.dot(forwardDir, velocityNormalize);
    //           if (angle<0.8) 
    //           { 
    //             this.m_stateController.MyRigibody.friction=15;
    //           }
    //           else
    //           {
    //             this.m_stateController.MyRigibody.friction=0;
    //           }    
    //     }
    //     this.m_stateController.MyAnimator.speed=velocityscaler/5;
      
    // }


}